import { KNOWLEDGE, ERROR_TOPIC } from '../../../constants/qualityFeedback';
import {
		SWITCH_QUALITY_FEEDBACK_VIEW,
		GET_QUALITY_FEEDBACK_BASIC_REQUEST,
		GET_QUALITY_FEEDBACK_BASIC_SUCCESS,
		GET_QUALITY_FEEDBACK_BASIC_FAILURE,
		GET_QUALITY_FEEDBACK_DETAIL_REQUEST,
		GET_QUALITY_FEEDBACK_DETAIL_SUCCESS,
		GET_QUALITY_FEEDBACK_DETAIL_FAILURE,
		SET_QUALITY_FEEDBACK_DETAIL_PAGEINDEX,
		SET_QUALITY_FEEDBACK_DETAIL_CURRENT_PAGEINDEX,
		} from '../actions/qualityFeedback';
import cloneDeep from 'lodash/cloneDeep';
import merge from 'lodash/merge';
export const INITIALIZE_STATE = {
	// 提示消息
	message:'',
	// 默认二级视图(布置作业,批改作业)
	view: KNOWLEDGE,
	basic:{
		isFetching:false,
		dataSource:[]
	},
	detail:{
		isFetching:false,
		dataSource:[],
		pageIndex:0,
		currentPageIndex:1
	}
	
};
export default function qualityFeedback(state = INITIALIZE_STATE, action = {}) {
	let { type, ...others } = action;
	switch(type) {
		case SWITCH_QUALITY_FEEDBACK_VIEW :
		{
			let { view } = others.payload;
			let newState = Object.assign({},state,{
				view
			})
			return newState;
		};
		case GET_QUALITY_FEEDBACK_BASIC_REQUEST :
		{
			let newState = merge({},state,{
				basic:{
					isFetching:true
				}
			});
			return newState;
		};
		case GET_QUALITY_FEEDBACK_BASIC_SUCCESS :
		{
			let { dataSource } = others.payload;
			let basic = cloneDeep(state.basic);
				Object.assign(basic,{
					dataSource,
					isFetching:false
				});
			let newState = Object.assign({},state,{
				basic
			});
			return newState;
		};
		case GET_QUALITY_FEEDBACK_BASIC_FAILURE :
		{
			let { message } = others.payload;
			let newState = merge({},state,{
				message,
				basic:{
					isFetching:false
				}
			});
			return newState;
		};
		case GET_QUALITY_FEEDBACK_DETAIL_REQUEST :
		{
			let newState = merge({},state,{
				detail:{
					isFetching:true
				}
			});
			return newState;
		};
		case GET_QUALITY_FEEDBACK_DETAIL_SUCCESS :
		{
			let { dataSource } = others.payload;
			let detail = cloneDeep(state.detail);
				Object.assign(detail,{
					dataSource,
					isFetching:false
				});
			let newState = Object.assign({},state,{
				detail
			});
			return newState;
		};
		case GET_QUALITY_FEEDBACK_DETAIL_FAILURE :
		{
			let { message } = others.payload;
			let newState = merge({},state,{
				message,
				detail:{
					isFetching:false
				}
			});
			return newState;
		};
		case SET_QUALITY_FEEDBACK_DETAIL_PAGEINDEX :
		{
			let { pageIndex } =others.payload;
			return merge({},state,{
				detail:{
					pageIndex
				}
			})
		};
		case SET_QUALITY_FEEDBACK_DETAIL_CURRENT_PAGEINDEX :
		{
			let { currentPageIndex } =others.payload;
			return merge({},state,{
				detail:{
					currentPageIndex
				}
			})
		};
		default: 
			return state;
	}
};